﻿
#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Reflection;
    using System.Text;

#endregion

namespace MarketPulse.BusinessLogic.Common.StateMachine
{
    [Serializable]
    internal class EnquiryStateBase : IEnquiryState
    {
        /// <summary>
        /// 
        /// </summary>
        protected Guid mEnquiryStateType;

        /// <summary></summary>
        protected string mVisitStateString;

        protected bool mThrowIfInvalid;

        internal EnquiryStateBase(bool throwIfInvalid)
        {
            mThrowIfInvalid = throwIfInvalid;
            mEnquiryStateType = Guid.Empty;
        }

        protected void InitVisitStateString()
        {
            mVisitStateString = VisitStatusTypeList.GetList().GetItemByKey(mEnquiryStateType).Value;
        }

        private void ThrowTransitionException()
        {
            StackTrace stackTrace = new StackTrace();
            StackFrame stackFrame = stackTrace.GetFrame(1);
            MethodBase methodBase = stackFrame.GetMethod();
            throw new EnquiryStateTransitionException(mVisitStateString, methodBase.Name);
        }

        public virtual bool Accept(IEnquiryStateContext state, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public Guid StateGuid
        {
            get
            {
                return mEnquiryStateType;
            }
        }

        public bool ThrowIfInvalid
        {
            get
            {
                return mThrowIfInvalid;
            }

            set
            {
                mThrowIfInvalid = value;
            }
        }
        

    }
}
